package top.jolyoulu.codegenweb.service.bll.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.jolyoulu.codegenweb.entity.bo.form.GlobalVariableMapFormBO;
import top.jolyoulu.codegenweb.entity.bo.query.GlobalVariableMapQueryBO;
import top.jolyoulu.codegenweb.entity.po.GlobalVariableMap;
import top.jolyoulu.codegenweb.entity.vo.GlobalVariableMapVO;
import top.jolyoulu.codegenweb.service.bll.ApiGlobalVariableMapService;
import top.jolyoulu.codegenweb.service.dal.GlobalVariableMapService;
import top.jolyoulu.modules.mybatisplusmodule.plugin.page.JlPage;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: JolyouLu
 * @Date: 2023/4/15 16:13
 * @Description
 */
@Slf4j
@Service
public class ApiGlobalVariableMapServiceImpl implements ApiGlobalVariableMapService {

    @Autowired
    private GlobalVariableMapService globalVariableMapService;

    @Override
    public JlPage<GlobalVariableMapVO> pageList(GlobalVariableMapQueryBO bo) {
        JlPage<GlobalVariableMapVO> res = JlPage.execute(bo.getPage(), bo.getPageSize(),
                () -> {
                    LambdaQueryWrapper<GlobalVariableMap> ew = new LambdaQueryWrapper<>();
                    List<GlobalVariableMap> list = globalVariableMapService.list(ew);
                    return list.stream().map(GlobalVariableMapVO::new).collect(Collectors.toList());
                });
        return res;
    }

    @Override
    public GlobalVariableMapVO getById(String id) {
        GlobalVariableMap res = globalVariableMapService.getById(id);
        return new GlobalVariableMapVO(res);
    }

    @Override
    public void add(GlobalVariableMapFormBO bo) {
        GlobalVariableMap globalVariableMap = new GlobalVariableMap();
        BeanUtils.copyProperties(bo,globalVariableMap);
        globalVariableMapService.save(globalVariableMap);
    }

    @Override
    public void update(GlobalVariableMapFormBO bo) {
        GlobalVariableMap globalVariableMap = new GlobalVariableMap();
        BeanUtils.copyProperties(bo,globalVariableMap);
        globalVariableMapService.updateById(globalVariableMap);
    }

    @Override
    public void delByIds(List<String> ids) {
        for (String id : ids) {
            globalVariableMapService.removeById(id);
        }
    }
}
